package com.wj.facedetect.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.wj.facedetect.R;
import com.wj.facedetect.activity.base.MainActivity;
import com.wj.facedetect.activity.login.LoginActivity;
import com.wj.facedetect.bean.settting.GetAllKuBean;
import com.wj.facedetect.commom.FaceDetectApplication;
import com.wj.facedetect.presenter.SettingPresenter;
import com.wj.facedetect.util.DialogUtil;
import com.wj.facedetect.util.PreferencesUtils;
import com.wj.facedetect.view.IBaseView;
import com.wj.facedetect.widget.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SettingFragment extends Fragment implements IBaseView, CanRefreshLayout.OnRefreshListener {
    private View mainView;
    private Spinner spinner;
    private ArrayAdapter<String> arrayAdapter;
    private ArrayList<String> arrayListKu;
    private List<GetAllKuBean.ResultsBean> results = new ArrayList<>();
    private TextView tvKuName;
    private TextView tvLogout;
    private LoadingDialog loadingDialog;
    private CanRefreshLayout canRefreshLayout;
    private SettingPresenter settingPresenter;
    private static final int REQ_GET_ALL_KU = 0x110;

    public SettingFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mainView = inflater.inflate(R.layout.fragment_setting, container, false);
        settingPresenter = new SettingPresenter(this);
        loadingDialog = new LoadingDialog(getActivity());
        initView();
        return mainView;
    }

    private void initView() {
        canRefreshLayout = mainView.findViewById(R.id.refresh);
        canRefreshLayout.setOnRefreshListener(this);
        spinner = mainView.findViewById(R.id.sp_ku);
        arrayListKu = new ArrayList<>();
        arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item, arrayListKu);
        arrayAdapter.setDropDownViewResource(R.layout.drop_style_item);
        spinner.setAdapter(arrayAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (results.size() > 0) {
                    PreferencesUtils.getInstance().putSharePre(PreferencesUtils.KU_ID, results.get(i).getId());
                    PreferencesUtils.getInstance().putSharePre(PreferencesUtils.KU_ID_NAME, results.get(i).getName());
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                if (results.size() > 0) {
                    PreferencesUtils.getInstance().putSharePre(PreferencesUtils.KU_ID, results.get(0).getId());
                    PreferencesUtils.getInstance().putSharePre(PreferencesUtils.KU_ID_NAME, results.get(0).getName());
                }
            }
        });
        tvKuName = mainView.findViewById(R.id.tv_ku);
        tvKuName.setText(PreferencesUtils.getInstance().getSharePreStr(PreferencesUtils.KU_ID_NAME));
        tvLogout = mainView.findViewById(R.id.tv_logout);
        tvLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showChooseDialog(getActivity(), "", "是否确认安全退出程序？", "", "", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        FaceDetectApplication.getApplication().exit();
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                        getActivity().finish();
                    }
                });

            }
        });
        canRefreshLayout.autoRefresh();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        settingPresenter.getModel().cancleRequest(this);
    }

    @Override
    public void onSuccess(int what, Object data, Object carryObj) {
        if (canRefreshLayout != null) {
            canRefreshLayout.refreshComplete();
        }
        ((MainActivity) getActivity()).dismissProgressDialog();
        switch (what) {
            case REQ_GET_ALL_KU:
                GetAllKuBean getAllKuBean = (GetAllKuBean) data;
                if (getAllKuBean != null) {
                    if (getAllKuBean.getRtn() == 0) {
                        results = getAllKuBean.getResults();
                        if (results.size() > 0) {
                            for (GetAllKuBean.ResultsBean bean : results) {
                                arrayListKu.add(bean.getName());
                            }
                            arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item, arrayListKu);
                            arrayAdapter.setDropDownViewResource(R.layout.drop_style_item);
                            spinner.setAdapter(arrayAdapter);
                            int kuId = PreferencesUtils.getInstance().getSharePreInt(PreferencesUtils.KU_ID);
                            int position = 0;
                            for (int i = 0; i < results.size(); i++) {
                                if (kuId == results.get(i).getId()) {
                                    position = i;
                                }
                            }
                            spinner.setSelection(position);
                        }
                    } else {
                        DialogUtil.showErrorMsgWithBack(getActivity(), "账户被迫下线，请重新登录", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                getActivity().startActivity(new Intent(getActivity(), LoginActivity.class));
                                getActivity().finish();
                            }
                        });
                    }
                }
                break;
        }
    }

    @Override
    public void onError(int what, Exception e, Object carryObj) {
        if (canRefreshLayout != null) {
            canRefreshLayout.refreshComplete();
        }
        ((MainActivity) getActivity()).dismissProgressDialog();

    }

    @Override
    public void onFail(int what, int errorCode, String errorMsg, Object carryObj) {
        if (canRefreshLayout != null) {
            canRefreshLayout.refreshComplete();
        }
        ((MainActivity) getActivity()).dismissProgressDialog();
    }

    @Override
    public void onRefresh() {
        ((MainActivity) getActivity()).showProgressDialog();
        settingPresenter.getAllKu(REQ_GET_ALL_KU, this);
    }
}
